package com.example.administrator.caigou51.activity;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.MessageDetailBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.basic.SuperDIYSwipeRefreshLayout;
import com.example.administrator.caigou51.recyclerCard.card.GongGaoTongZhiCard;
import com.example.administrator.caigou51.recyclerCard.card.HuoDongTongZhiCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.List;

/**
 * Created by Administrator on 2016/8/25/025.
 */
public class MessageGongGaoActivity extends GBaseActivity{
    MaterialListView material_listview;
    SuperDIYSwipeRefreshLayout superSwipeRefresh;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_super_diyswiperefresh_materiallistview);
        findViewByIds();
        initDatas();
        //消息类型0活动通知 1订单提醒2公告通知
        TaskGetMessageList(false,"2",superSwipeRefresh.getPageSize()+"",superSwipeRefresh.getPage(false)+"");
    }

    private void findViewByIds() {
        material_listview= (MaterialListView) findViewById(R.id.listview);
        superSwipeRefresh= (SuperDIYSwipeRefreshLayout) findViewById(R.id.superSwipeRefresh);
        superSwipeRefresh.setOnPullRefreshListener(new SuperDIYSwipeRefreshLayout.OnPullRefreshListener() {
            @Override
            public void onRefresh() {
                TaskGetMessageList(false,"2",superSwipeRefresh.getPageSize()+"",superSwipeRefresh.getPage(false)+"");

            }

            @Override
            public void onPullDistance(int distance) {

            }

            @Override
            public void onPullEnable(boolean enable) {

            }
        });
        superSwipeRefresh.setOnPushLoadMoreListener(new SuperDIYSwipeRefreshLayout.OnPushLoadMoreListener() {
            @Override
            public void onLoadMore() {
                TaskGetMessageList(true,"2",superSwipeRefresh.getPageSize()+"",superSwipeRefresh.getPage(true)+"");
            }

            @Override
            public void onPushDistance(int distance) {

            }

            @Override
            public void onPushEnable(boolean enable) {

            }
        });
    }
    private void fillAraayList(boolean isload, List<MessageDetailBean.DataEntity> list) {

        if (!isload)
        {
            material_listview.clear();
        }
        for (int i=0;i<list.size();i++)
        {
            GongGaoTongZhiCard gongGaoTongZhiCard=new GongGaoTongZhiCard(this);
            gongGaoTongZhiCard.setDataEntity(list.get(i));
            material_listview.add(gongGaoTongZhiCard);
        }

    }
    private void initDatas() {
        setTopTitle("公告通知");
        getRightImageView().setVisibility(View.GONE);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }
    //消息类型0活动通知 1订单提醒2公告通知
    private void TaskGetMessageList(final boolean isloadmore,String type,String pageSize,String page) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), material_listview, isloadmore, superSwipeRefresh,
                Constant.getRootUrl() + Constant.Action.Action_MessageDetailList, Constant.Action.Action_MessageDetailList, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_MessageDetailList, type, App.getUserBean().getUserid()+"", App.getUserBean().getSession_id()+"",pageSize,page), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        List<MessageDetailBean.DataEntity> messageDetailBeanList = JSON.parseArray(result.getData().toString(), MessageDetailBean.DataEntity.class);
                        fillAraayList(isloadmore,messageDetailBeanList);
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
}
